package com.approval.controller;

import com.approval.dto.*;
import com.approval.security.RequirePermission;
import com.approval.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/permissions")
@CrossOrigin(origins = "*")
@RequirePermission(resource = "permission", action = "list")
public class PermissionManagementController {
    
    @Autowired
    private PermissionService permissionService;
    
    /**
     * 创建权限
     */
    @PostMapping
    public ResponseEntity<Result<PermissionResponse>> createPermission(@Valid @RequestBody PermissionCreateRequest request) {
        try {
            PermissionResponse permission = permissionService.createPermission(request);
            return ResponseEntity.ok(Result.success(permission));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 更新权限
     */
    @PutMapping("/{id}")
    public ResponseEntity<Result<PermissionResponse>> updatePermission(@PathVariable Long id, @Valid @RequestBody PermissionUpdateRequest request) {
        try {
            PermissionResponse permission = permissionService.updatePermission(id, request);
            return ResponseEntity.ok(Result.success(permission));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> deletePermission(@PathVariable Long id) {
        try {
            permissionService.deletePermission(id);
            return ResponseEntity.ok(Result.success());
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据ID获取权限
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<PermissionResponse>> getPermissionById(@PathVariable Long id) {
        try {
            PermissionResponse permission = permissionService.getPermissionById(id);
            return ResponseEntity.ok(Result.success(permission));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 分页查询权限
     */
    @GetMapping
    public ResponseEntity<Result<PageResponse<PermissionResponse>>> getPermissions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String permissionName,
            @RequestParam(required = false) String permissionType,
            @RequestParam(required = false) String resource,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "id") String sortField,
            @RequestParam(defaultValue = "asc") String sortOrder) {
        try {
            PageRequestDto pageRequest = new PageRequestDto();
            pageRequest.setPage(page);
            pageRequest.setSize(size);
            pageRequest.setSortField(sortField);
            pageRequest.setSortOrder(sortOrder);
            
            PageResponse<PermissionResponse> permissions = permissionService.getPermissions(pageRequest, permissionName, permissionType, resource, status);
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取所有权限
     */
    @GetMapping("/all")
    public ResponseEntity<Result<List<PermissionResponse>>> getAllPermissions() {
        try {
            List<PermissionResponse> permissions = permissionService.getAllPermissions();
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取权限树
     */
    @GetMapping("/tree")
    public ResponseEntity<Result<List<PermissionResponse>>> getPermissionTree() {
        try {
            List<PermissionResponse> tree = permissionService.getPermissionTree();
            return ResponseEntity.ok(Result.success(tree));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取菜单权限
     */
    @GetMapping("/menu")
    public ResponseEntity<Result<List<PermissionResponse>>> getMenuPermissions() {
        try {
            List<PermissionResponse> permissions = permissionService.getMenuPermissions();
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取操作权限
     */
    @GetMapping("/operation")
    public ResponseEntity<Result<List<PermissionResponse>>> getOperationPermissions() {
        try {
            List<PermissionResponse> permissions = permissionService.getOperationPermissions();
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据权限类型获取权限列表
     */
    @GetMapping("/type/{type}")
    public ResponseEntity<Result<List<PermissionResponse>>> getPermissionsByType(@PathVariable String type) {
        try {
            List<PermissionResponse> permissions = permissionService.getPermissionsByType(type);
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据父权限ID获取子权限列表
     */
    @GetMapping("/parent/{parentId}")
    public ResponseEntity<Result<List<PermissionResponse>>> getPermissionsByParentId(@PathVariable Long parentId) {
        try {
            List<PermissionResponse> permissions = permissionService.getPermissionsByParentId(parentId);
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取根权限列表
     */
    @GetMapping("/root")
    public ResponseEntity<Result<List<PermissionResponse>>> getRootPermissions() {
        try {
            List<PermissionResponse> permissions = permissionService.getRootPermissions();
            return ResponseEntity.ok(Result.success(permissions));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
}